" check the vim supports python
if !has('python')
    echo 'Error: Required vim compile with +python'
    finish
endif

python << EOF

import vim
import re
import requests
from bs4 import BeautifulSoup as soup
from collections import defaultdict

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

def gethtml(keyword):
    url = u'http://www.youdao.com/w/{0}/#keyfrom=dict2.top'.format(unicode(keyword))
    headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}
    rep = requests.get(url, headers= headers)
    return rep.content
    
def getcleanstr(args):
    if isinstance(args, basestring):
        args=args.strip()
        args=' '.join(args.split('\n'))
        args=re.sub(re.compile('\s\s+'), ' ', args)        
        return args        

class Base:
    tag=''
    attr={}
    def parse(self, *args, **kwargs):
        pass

class MainTrans(Base):
    tag='div'
    attrs={'class': 'trans-container'}
    def parse(self, root):
        results=[]
        items= root.find(name=self.tag, attrs=self.attrs)

        for i in items.find_all('p', attrs={'class': 'wordGroup'}):
            results.append(getcleanstr(i.text))

        for i in items.find_all('li'):
            results.append(getcleanstr(i.text))

        for i in items.find_all('p', attrs={'class':'additional'}):
            results.append(getcleanstr(i.text))
            
        return ('基本释义', results)

class tWebTrans(Base):
    tag= 'div'
    attrs={'id': 'tWebTrans'}
    def parse(self, root):
        results=[]
        items = root.find(name = self.tag, attrs=self.attrs)
        if items:
            for i in items.find_all('div', attrs={'class': 'wt-container'}):
                results.append(getcleanstr(i.text))

            # <div id="webPhrase" class="pr-container more-collapse">
            webphrases = items.find('div', attrs={'id': 'webPhrase'})
            if webphrases:
                title = webphrases.find('div', attrs={'class': 'title'})
                results.append(title.text.strip())
    
                # <p class="wordGroup">
                for i in webphrases.find_all('p', attrs={'class': 'wordGroup'}):
                    results.append(getcleanstr(i.text))

        return ('网络释义', results)

class authDictTrans(Base):
    tag='div'
    attrs={'id': 'authDictTrans'}
    def parse(self, root):
        results=[]
        items = root.find(name = self.tag, attrs= self.attrs)
        if items:
            for item in items.find_all('h4', {'class': 'wordGroup'}):
                results.append(getcleanstr(item.text))
                nextul=item.findNextSibling('ul')
                if nextul is not None:
                    # for i in nextul.find_all(['span', 'p', 'li'], recursive=True):
                    for i in nextul.find_all('li', recursive=False):
                        results.append('\t'+ getcleanstr(i.text))

        # print '\n'.join(results)
        return ('21世纪大英汉词典', results)

class collinsResult(Base):
    tag='div'
    attrs={'id': 'collinsResult'}
    def parse(self,root):
        results=[]
        items =root.find(name = self.tag, attrs=self.attrs)

        if items is not None:
        #<div class="wt-container">
            for i in items.find_all('div', attrs={'class': 'wt-container'}):
                results.append(i.span.text)

                for j in i.find_all('div'):
                    # print i.attrs
                    if j.attrs=={'class': ['collinsMajorTrans']}:
                        results.append(getcleanstr(j.text))
                    if j.attrs== {'class': ['exampleLists']}:
                        results.append('\t'+getcleanstr(j.text))
            # print '\n'.join(results)

        return ('柯林斯英汉双解大词典',results)

class wordGroup(Base):
    tag='div'
    attrs={'id': 'wordGroup'}
    def parse(self,root):
        results=[]
        items = root.find(name= self.tag, attrs=self.attrs)
        #<p class="wordGroup">
        if items is not None:
            for i in items.find_all('p', attrs={'class': 'wordGroup'}):
                results.append(getcleanstr(i.text))

        return ('词组短语', results)

# <div id="synonyms" class="trans-container tab-content hide">
class synonyms(Base):
    tag= 'div'
    attrs={'id': 'synonyms'}
    def parse(self, root):
        results=[]
        items= root.find(name= self.tag , attrs=self.attrs)
        if items is not None:
            for i in items.find_all(re.compile('li')):
                results.append(i.text.strip())

                #next sibling
                ptag=i.findNextSibling('p')
                results.append(getcleanstr(ptag.text))

        # print '\n'.join(results)
        return ('同近义词', results)

# <div id="relWordTab" class="trans-container tab-content hide">
class relWordTab(Base):
    tag= 'div'
    attrs={'id': 'relWordTab'}
    def parse(self, root):
        results=[]
        # <p class="wordGroup">
        items=root.find(name= self.tag, attrs=self.attrs)
        if items is not None:
            for i in items.find_all('p', attrs={'class': 'wordGroup'}):
                results.append(getcleanstr(i.text))
        return ('同根词', results)

class bilingual(Base):
    tag= 'div'
    attrs={'id': 'bilingual'}
    def parse(self, root):
        results=[]
        items =root.find(name = self.tag, attrs=self.attrs)
        if items is not None:
            for i in items.find_all('li'):
                results.append(getcleanstr(i.text))
        return ('双语例句', results)

class ydTrans(Base):
    tag= 'div'
    attrs={'id': 'ydTrans'}
    def parse(self, root):
        results=[]
        items = root.find(name= self.tag, attrs=self.attrs)
        if items is not None:
            for i in items.find_all('div', {'class': 'trans-container'}):
                results.append(getcleanstr(i.text))
        return ('有道翻译' , results)

class Parser:

    def __init__(self, kw):
        self._kw= kw
        self._html = gethtml(kw)
        self._root= soup(self._html, 'lxml')
        self._dict={}

    def parse(self):
        results=[]
        results.append(bilingual().parse(self._root))
        results.append(relWordTab().parse(self._root))
        results.append(synonyms().parse(self._root))
        results.append(wordGroup().parse(self._root))
        results.append(collinsResult().parse(self._root))
        results.append(authDictTrans().parse(self._root))
        results.append(tWebTrans().parse(self._root))
        results.append(MainTrans().parse(self._root))
        results.append(ydTrans().parse(self._root))
        for k,v in results:
            # self._dict[k]=v
            self._dict.update({k: v})


def Main(args):
    del vim.current.buffer[:]
    
    p=Parser(args)
    p.parse()
    for k, v in p._dict.items():
        if v:
            vim.current.buffer.append(str(k)+' --> ')
            for item in v:
                item=re.sub('\n','',item)
                vim.current.buffer.append('\t'+item)
            vim.current.buffer.append(' ')
            
EOF

command! -nargs=+ Ydao python Main(<q-args>)